<template>
  <div class="goods">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <!-- 栅格布局 每一行col总共24份 当前占8份  gutter每一格中间的空隙 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <!-- 一个输入框  clear清空触发事件 -->
            <el-input ref="input" v-model="inputtext" @clear="init" clearable placeholder="请输入内容" class="input-with">
              <!-- slot="append" 尾部的插槽 -->
              <el-button @click="init" slot="append" icon="el-icon-search"></el-button>
            </el-input>
          </div>
        </el-col>
        <!-- 当前栅格占4 -->
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-button @click="goaddpage" type="primary">添加用户</el-button>
          </div>
        </el-col>
      </el-row>
      <!-- 表格 tableData 绑定的数据对象 边框 stripe隔行变色-->
      <el-table :data="tableData" :border="true" style="width: 100%">
        <!-- 每一行 index表示当前行是索引-->
        <el-table-column label="#" type="index"></el-table-column>
        <!-- prop="username" 这个属性表示表格内容采用table的 :data对象内的对应属性-->
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="价格" prop="goods_price" width=80></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width=80></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="150">
          <template slot-scope="scope">
            {{ scope.row.add_time | time }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <!-- @click="setitem(scope.row)" -->
            <el-button  size="mini" type="primary" icon="el-icon-edit"></el-button>
            <el-button @click="deleteitem(scope.row.goods_id)" size="mini" type="danger" icon="el-icon-delete">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="gettableinfo.pagenum" :page-sizes="[2, 5, 10]" :page-size="gettableinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

  </div>
</template>

<script>
import moment from 'moment';
export default {
  data() {
    return {
      gettableinfo: {
        query: '',
        //当前页
        pagenum: 1,
        //每一页条数
        pagesize: 5,
      },
      total: 0,
      inputtext: '',
      tableData: [],
    }
  },
  filters: {
    time(v) {
      return moment(parseInt(v)).format('YYYY-MM-DD')
    }
  },
  created() {
    this.initpagedata();
  },
  methods: {
    async initpagedata() {
      let { data: res } = await this.$http.get(`goods`, { params: this.gettableinfo });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      console.log(res.data);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
    //输入框清空 //确定
    init() {
      this.gettableinfo.query = this.inputtext;
      this.initpagedata();
    },
    //添加用户
    adduser_dialog() {
      console.log('添加');
    },
    //每页总数变
    handleSizeChange(v) {
      this.gettableinfo.pagenum = 1;
      this.gettableinfo.pagesize = v;
      this.initpagedata();
    },
    //页数变
    handleCurrentChange(v) {
      this.gettableinfo.pagenum = v;
      this.initpagedata();
    },
    //删除
    deleteitem(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //删除操作
        let { data: res } = await this.$http.delete(`goods/${id}`);
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
        this.initpagedata();
        this.$message.success('删除成功');
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      });
    },
    //去添加页面
    goaddpage() {
      // adduser_dialog = true;
      this.$router.push('/goods/add');
    },

  },
}
</script>

<style>
</style>